package com.muqi.app.user.address;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.muqi.app.qlearn.student.R;
import com.muqi.app.user.address.ScrollerNumberPicker.OnSelectListener;

/**
 * 城市Picker
 * 
 * @author zd
 * 
 */
public class CityPicker extends LinearLayout {
	/** 滑动控件 */
	private ScrollerNumberPicker provincePicker;
	private ScrollerNumberPicker cityPicker;
	private ScrollerNumberPicker counyPicker;
	/** 选择监听 */
	private OnSelectingListener onSelectingListener;
	/** 刷新界面 */
	private static final int REFRESH_VIEW = 0x001;
	/** 临时日期 */
	private int tempProvinceIndex = 0;
	private int temCityIndex = 0;
	private int tempCounyIndex = 0;
	private List<Cityinfo> province_list = new ArrayList<Cityinfo>();

	private CitycodeUtil citycodeUtil;
	private String city_code_string;
	private String city_string;
	private String city_id;
	private int mode;

	public CityPicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.province_list = SelectAddressWindow.province_list;
		this.mode = SelectAddressWindow.mode;
	}

	public CityPicker(Context context, List<Cityinfo> province_list) {
		super(context);
		this.province_list = province_list;
		this.mode = SelectAddressWindow.mode;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		citycodeUtil = CitycodeUtil.getSingleton();
		if (mode == 3) {
			LayoutInflater.from(getContext()).inflate(R.layout.city_picker3,
					this);
			provincePicker = (ScrollerNumberPicker) findViewById(R.id.province);
			cityPicker = (ScrollerNumberPicker) findViewById(R.id.city);
			counyPicker = (ScrollerNumberPicker) findViewById(R.id.couny);
		}
		if (mode == 2) {
			LayoutInflater.from(getContext()).inflate(R.layout.city_picker2,
					this);
			provincePicker = (ScrollerNumberPicker) findViewById(R.id.province);
			cityPicker = (ScrollerNumberPicker) findViewById(R.id.city);
		}
		// 获取控件引用
		provincePicker.setData(citycodeUtil.getProvince(province_list));
		provincePicker.setDefault(0);
		cityPicker.setData(citycodeUtil.getCity(province_list, 0));
		cityPicker.setDefault(0);
		if (mode == 3) {
			counyPicker.setData(citycodeUtil.getCouny(province_list, 0, 0));
			counyPicker.setDefault(0);
		}
		provincePicker.setOnSelectListener(new OnSelectListener() {

			@Override
			public void endSelect(int id, String text) {
				System.out.println("id-->" + id + "text----->" + text);
				if (text.equals("") || text == null)
					return;

				System.out.println("endselect");
				// 城市数组
				cityPicker.setData(citycodeUtil.getCity(province_list, id));
				cityPicker.setDefault(0);
				if (mode == 3) {
					// TODO miss a data;
					counyPicker.setData(citycodeUtil.getCouny(province_list,
							id, 0));
					counyPicker.setDefault(0);
				}
				int lastDay = Integer.valueOf(provincePicker.getListSize());
				if (id > lastDay) {
					provincePicker.setDefault(lastDay - 1);
				}

				tempProvinceIndex = id;
				Message message = new Message();
				message.what = REFRESH_VIEW;
				handler.sendMessage(message);
			}

			@Override
			public void selecting(int id, String text) {
			}
		});
		cityPicker.setOnSelectListener(new OnSelectListener() {

			@Override
			public void endSelect(int id, String text) {
				if (text.equals("") || text == null)
					return;
				String selectDay = provincePicker.getSelectedText();
				if (selectDay == null || selectDay.equals(""))
					return;
				if (mode == 3) {
					counyPicker.setData(citycodeUtil.getCouny(province_list,
							tempProvinceIndex, id));
					counyPicker.setDefault(0);
				}
				int lastDay = Integer.valueOf(cityPicker.getListSize());
				if (id > lastDay) {
					cityPicker.setDefault(lastDay - 1);
				}

				temCityIndex = id;
				Message message = new Message();
				message.what = REFRESH_VIEW;
				handler.sendMessage(message);
			}

			@Override
			public void selecting(int id, String text) {

			}
		});
		if (mode == 3) {
			counyPicker.setOnSelectListener(new OnSelectListener() {

				@Override
				public void endSelect(int id, String text) {
					if (text.equals("") || text == null)
						return;
					String selectDay = provincePicker.getSelectedText();
					if (selectDay == null || selectDay.equals(""))
						return;
					String selectMonth = cityPicker.getSelectedText();
					if (selectMonth == null || selectMonth.equals(""))
						return;
					// 城市数组
					city_code_string = citycodeUtil.getCouny_list_code()
							.get(id);
					int lastDay = Integer.valueOf(counyPicker.getListSize());
					if (id > lastDay) {
						counyPicker.setDefault(lastDay - 1);
					}

					tempCounyIndex = id;
					Message message = new Message();
					message.what = REFRESH_VIEW;
					handler.sendMessage(message);
				}

				@Override
				public void selecting(int id, String text) {
				}
			});
		}
	}

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case REFRESH_VIEW:
				if (onSelectingListener != null)
					onSelectingListener.selected(true);
				break;
			default:
				break;
			}
		}

	};

	public void setOnSelectingListener(OnSelectingListener onSelectingListener) {
		this.onSelectingListener = onSelectingListener;
	}

	public String getCity_code_string() {
		return city_code_string;
	}

	public String getCity_string() {
		if (mode == 3) {
			city_string = provincePicker.getSelectedText() + " "
					+ cityPicker.getSelectedText() + " "
					+ counyPicker.getSelectedText();
		} else {
			city_string = provincePicker.getSelectedText() + " "
					+ cityPicker.getSelectedText();
		}
		return city_string;
	}

	public String getCity_id() {
		if (mode == 3) {
			city_id = provincePicker.getSelected() + ","
					+ cityPicker.getSelected() + ","
					+ counyPicker.getSelected();
		} else {
			city_id = provincePicker.getSelected() + ","
					+ cityPicker.getSelected();
		}
		return city_id;
	}

	public interface OnSelectingListener {
		public void selected(boolean selected);
	}
}
